package utils

import (
	"bytes"
	"os"
	"os/exec"
	"strconv"
)

func FindProcessByName(name string) ([]*os.Process, error) {
	cmd := exec.Command("bash", "-c", "ps -ef|grep "+name+"|grep -v grep|awk '{print $2}'")
	out, err := cmd.Output()
	if err != nil {
		return nil, err
	}
	outList := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
	var processList []*os.Process
	for _, v := range outList {
		pid, err := strconv.Atoi(string(bytes.TrimSpace(v)))

		if err != nil {
			return nil, err
		}

		process, err := os.FindProcess(pid)
		if err != nil {
			return nil, err
		}
		processList = append(processList, process)
	}
	return processList, err
}
